package com.ultrapower.ctool.xn_app;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.hh.timeselector.timeutil.datedialog.DateListener;
import com.hh.timeselector.timeutil.datedialog.TimeConfig;
import com.hh.timeselector.timeutil.datedialog.TimeSelectorDialog;
import com.ultrapower.ctool.ConfUtils;
import com.ultrapower.ctool.bean.WangGeBean;
import com.ultrapower.ctool.core.CustomApplication;
import com.ultrapower.ctool.http.BaseActivity;
import com.ultrapower.ctool.model.RegisterRolePirVO;

import org.feezu.liuli.timeselector.TimeSelector;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class AddRiChangActivity extends BaseActivity implements View.OnClickListener {
    private EditText dwid;//单位(编号)
    private TextView checkTime;//巡查时间
    private TextView remarks;//备注
    private ImageView time;//获取当前时间
    private Button save;//保存
    private Button cancle;//取消
    //private OkHttpClient client = new OkHttpClient();
    private Gson gson = new Gson();
    private CustomApplication application;
    private AdapterView.OnItemSelectedListener OnCatSpinnerCL = new AdapterView.OnItemSelectedListener() {
        public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
            ((TextView) parent.getChildAt(0)).setTextColor(Color.BLACK);
            ((TextView) parent.getChildAt(0)).setTextSize(18);
        }

        public void onNothingSelected(AdapterView<?> parent) {

        }
    };
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    Toast.makeText(getApplicationContext(), "添加成功", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(AddRiChangActivity.this, RiChangeActivity.class);
                    startActivity(intent);
                    finish();
                    break;
                case 0:
                    Toast.makeText(getApplicationContext(), "系统出现异常", Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_ri_chang);
        application = (CustomApplication) getApplication();
        init();
    }

    protected void init() {
        dwid = (EditText) findViewById(R.id.dwid_addrc);
        checkTime = (TextView) findViewById(R.id.checkTime_addrc);
        remarks = (TextView) findViewById(R.id.remarks_addrc);
        time = (ImageView) findViewById(R.id.checkTime_addrc_img);
        save = (Button) findViewById(R.id.save_add_rc);
        cancle = (Button) findViewById(R.id.cancle_add_rc);
        time.setOnClickListener(this);
        save.setOnClickListener(this);
        cancle.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.checkTime_addrc_img:
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                String date = sdf.format(new Date());
                showDatePickDialog(TimeConfig.YEAR_MONTH_DAY_HOUR_MINUTE, date);
                break;
            case R.id.save_add_rc:

                String dwidStr = dwid.getText().toString();
                String timeStr = checkTime.getText().toString();
                String remarkStr = remarks.getText().toString();
                if (//"".equals(dwidStr)
                    //||
                        "".equals(timeStr)
                    //|| "".equals(remarkStr)
                        ) {
                    Toast.makeText(getApplicationContext(), "请填写完整信息", Toast.LENGTH_SHORT).show();
                } else {
                    final Map<String, String> map = new HashMap<String, String>();
                    map.put("dwid", dwid.getText().toString().trim());
                    map.put("checktime", checkTime.getText().toString().trim() + ":00");
                    map.put("remarks", remarks.getText().toString().trim());
                    new Thread(new Runnable() {

                        @Override
                        public void run() {
                            Response enttyperesponse = null;
                            try {
                                OkHttpClient client = new OkHttpClient();
                                FormBody.Builder builder = new FormBody.Builder()
                                        .add("userId", String.valueOf(application.getLoginUser().getLogin_id()));
                                for (Map.Entry<String, String> m : map.entrySet()) {
                                    builder.add(m.getKey(), m.getValue());
                                }
                                RequestBody resultRequestBody = builder.build();
                                Request enttypeRequest = new Request.Builder().url(ConfUtils.SERVICE_URL + "form/doAdd/c_aj_rcxc").post(resultRequestBody).build();
                                enttyperesponse = client.newCall(enttypeRequest).execute();
                                if (enttyperesponse.isSuccessful()) {
                                    String result = enttyperesponse.body().string();
                                    Map<String, Object> resultMap = gson.fromJson(result, Map.class);
                                    System.out.println(resultMap);
                                    if ((boolean) resultMap.get("success")) {

                                        Message message = new Message();
                                        message.what = 1;
                                        handler.sendMessage(message);


                                    } else {
                                        Message message = new Message();
                                        message.what = 0;
                                        handler.sendMessage(message);
                                    }
                                }
                            } catch (Exception e) {
                                Message message = new Message();
                                message.what = 0;
                                handler.sendMessage(message);
                                e.printStackTrace();
                            } finally {
                                if (enttyperesponse != null) enttyperesponse.close();
                            }
                        }
                    }).start();
                }
                break;
            case R.id.cancle_add_rc:
                finish();
                break;
        }

    }

    private void showDatePickDialog(int type, String date) {
        TimeSelectorDialog dialog = new TimeSelectorDialog(this);
        //设置标题
        dialog.setTimeTitle("选择时间:");
        //显示类型
        dialog.setIsShowtype(TimeConfig.YEAR_MONTH_DAY_HOUR_MINUTE);
        //默认时间
        dialog.setCurrentDate(date);
        //隐藏清除按钮
        dialog.setEmptyIsShow(false);
        dialog.setDateListener(new DateListener() {
            @Override
            public void onReturnDate(String time, int year, int month, int day, int hour, int minute, int isShowType) {
                checkTime.setText(time);
            }

            @Override
            public void onReturnDate(String empty) {
                checkTime.setText(empty);
            }
        });
        dialog.show();
    }
}
